package spring.boot.controller;

import java.util.Collection;

import javax.servlet.http.HttpServletRequest;

import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.context.SecurityContextImpl;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.web.authentication.WebAuthenticationDetails;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

@Controller
public class IndexController {

	//@Autowired
	//private SecurityContext securityContext;
	
	@RequestMapping("welcome")
	public String welcome(HttpServletRequest request){
		SecurityContextImpl securityContext = (SecurityContextImpl) request
				.getSession().getAttribute("SPRING_SECURITY_CONTEXT");
		String name = securityContext.getAuthentication().getName();
		Object password = securityContext.getAuthentication().getCredentials();
		WebAuthenticationDetails details = (WebAuthenticationDetails) securityContext.getAuthentication().getDetails();
		System.out.println("name:"+name);
		System.out.println("password:"+password);
		System.out.println("details:"+details.getRemoteAddress());
		System.out.println("details:"+details.getSessionId());
		System.out.println("--------------------------------");
		
		UserDetails userDetails = (UserDetails) SecurityContextHolder.getContext().getAuthentication() .getPrincipal();
		System.out.println(userDetails.getUsername());
		System.out.println(userDetails.getPassword());
		Collection<? extends GrantedAuthority> collection = userDetails.getAuthorities();
		System.out.println(collection.iterator().next());
		
		
		return "welcome";
	}
}
